const express = require('express')
const fileUpload = require('express-fileupload')
const fs = require('fs')
const path = require('path')

const app = express()

app.use(fileUpload())



// post请求通过postman 来发送
app.post('/upload', function (request, response) {
  // 定义文件名
  console.log(request.files);

  const fileName = request.files.icon.name
  // 路径拼接
  const fullPath = path.join(__dirname, './source/', fileName)
  console.log(request.files.icon.data);
  request.files.icon.mv(fullPath,(err)=>{
    if (!err) {
      console.log('文件上传成功');
      fs.readFile(fullPath,function (err,data) {
        if (!err) {
          response.send(data)
        }
      })
    }
  })

  // response.send('data')
})

app.listen(8848, function (error) {
  if (!error) {
    console.log('服务器已开启，通过http://localhost:8848/ 访问');
    return
  }


})
// console.log(app);
